package com.summer.springboot.jdbc.jta.repository;

import com.summer.springboot.jdbc.jta.entity.Log;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class LogRepository {

    @Resource
    @Qualifier("secondJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    public void save(Log log) {
        jdbcTemplate.update("insert into log(ip, log_time, method) values(?, ?, ?)",
                log.getId(),
                log.getLogTime(),
                log.getMethod());
    }

    public void deleteById(Long id) {
        jdbcTemplate.update("delete from log where id = ?", id);
    }

    public List<Log> getAll() {
        return jdbcTemplate.query("select * from log", new BeanPropertyRowMapper<>(Log.class));
    }

    public Log getById(Long id) {
        return jdbcTemplate.queryForObject("select * from user where id = ?", new Object[]{id}, new BeanPropertyRowMapper<>(Log.class));
    }

}
